<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的本地化功能" tabindex="-1"><a class="header-anchor" href="#laravel-的本地化功能"><span>Laravel 的本地化功能</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#defining-translation-strings">定义翻译字符串</a>
<ul>
<li><a href="#using-short-keys">使用短键</a></li>
<li><a href="#using-translation-strings-as-keys">使用翻译字符串作为键</a></li>
</ul>
</li>
<li><a href="#retrieving-translation-strings">检索翻译字符串</a>
<ul>
<li><a href="#replacing-parameters-in-translation-strings">翻译语句中的参数替换</a></li>
<li><a href="#pluralization">复数</a></li>
</ul>
</li>
<li><a href="#overriding-package-language-files">重写扩展包的语言文件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 的本地化功能为在应用程序中支持多种语言提供方便的方法来检索各种语言的字符串。语言字符串存储在 <code v-pre>resources/lang</code> 目录下的文件里。在此目录中，但凡应用支持的语言都应该有一个对应的子目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>resources</span>
<span class="line">    <span class="token operator">/</span>lang</span>
<span class="line">        <span class="token operator">/</span>en</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line">        <span class="token operator">/</span>es</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有语言文件只返回键值对数组，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="区域设置" tabindex="-1"><a class="header-anchor" href="#区域设置"><span>区域设置</span></a></h3>
<p>应用的默认语言保存在 <code v-pre>config/app.php</code> 配置文件中。你可以根据需要修改当前设置。还可以使用 <code v-pre>App</code> Facade 的 <code v-pre>setLocale</code> 方法动态地更改当前语言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'welcome/{locale}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">setLocale</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以设置 「备用语言」 ，它将会在当前语言不包含给定的翻译字符串时被使用。像默认语言一样，备用语言也可以在 <code v-pre>config/app.php</code> 配置文件设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'fallback_locale'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="确定当前语言环境" tabindex="-1"><a class="header-anchor" href="#确定当前语言环境"><span>确定当前语言环境</span></a></h4>
<p>你可以使用 <code v-pre>App</code> Facade 的 <code v-pre>getLocale</code> 及 <code v-pre>isLocale</code> 方法确定当前的区域设置或者检查语言环境是否为给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$locale</span> <span class="token operator">=</span> <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">getLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">isLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定义翻译字符串" tabindex="-1"><a class="header-anchor" href="#定义翻译字符串"><span>定义翻译字符串</span></a></h2>
<h3 id="使用短键" tabindex="-1"><a class="header-anchor" href="#使用短键"><span>使用短键</span></a></h3>
<p>通常，翻译字符串存放在 <code v-pre>resources/lang</code> 目录下的文件里。在此目录中但凡应用支持的语言都应该有一个对应的子目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>resources</span>
<span class="line">    <span class="token operator">/</span>lang</span>
<span class="line">        <span class="token operator">/</span>en</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line">        <span class="token operator">/</span>es</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有语言文件只返回键值对数组，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// resources/lang/en/messages.php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用翻译字符串作为键" tabindex="-1"><a class="header-anchor" href="#使用翻译字符串作为键"><span>使用翻译字符串作为键</span></a></h3>
<p>对于有大量翻译需求的应用， 如果每一条翻译语句都使用 「短键」 来定义，那么当你在视图中尝试去引用这些 「短键」 的时候，很容易变得混乱。因此，Laravel 也提供支持使用字符串的「默认」翻译作为关键字定义翻译字符串。</p>
<p>使用翻译字符串作为键的翻译文件作为 JSON 文件存储在 <code v-pre>resources/lang</code> 目录中。例如，如果你的应用中有西班牙语翻译，你应该新建一个 <code v-pre>resources/lang/es.json</code> 文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"I love programming."</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Me encanta programar."</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="检索翻译字符串" tabindex="-1"><a class="header-anchor" href="#检索翻译字符串"><span>检索翻译字符串</span></a></h2>
<p>你可以使用辅助函数 <code v-pre>__</code> 从语言文件中检索，<code v-pre>__</code> 方法接受翻译字符串的文件名和键值作为其第一个参数。例如，让我们检索 <code v-pre>resources/lang/messages.php</code> 语言文件中的 <code v-pre>welcome</code> 翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'I love programming.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果使用 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade 模板引擎</a>，可以在视图文件中使用 <code v-pre>{{ }}</code> 语法或者使用 <code v-pre>@lang</code> 指令来打印翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">lang</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果指定的翻译字符串不存在，<code v-pre>__</code> 方法则会简单地返回指定的翻译字符串键名。所以，如果上述示例中的翻译字符串键不存在，那么 <code v-pre>__</code> 方法则会返回 <code v-pre>messages.welcome</code> 。</p>
<h3 id="翻译语句中的参数替换" tabindex="-1"><a class="header-anchor" href="#翻译语句中的参数替换"><span>翻译语句中的参数替换</span></a></h3>
<p>如果需要也可以在翻译字符串中定义占位符。所有的占位符都有一个前缀 <code v-pre>:</code>。例如，你可以使用持有人名称定义欢迎消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :name'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在 <code v-pre>__</code> 方法中传递一个数组作为第二个参数，它会将数组的值替换到翻译字符串的占位符中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你的占位符中包含了首字母大写或者全体大写，翻译过来的内容也会做相应的处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :NAME'</span><span class="token punctuation">,</span> <span class="token comment">// Welcome, DAYLE</span></span>
<span class="line"><span class="token string single-quoted-string">'goodbye'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Goodbye, :Name'</span><span class="token punctuation">,</span> <span class="token comment">// Goodbye, Dayle</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复数" tabindex="-1"><a class="header-anchor" href="#复数"><span>复数</span></a></h3>
<p>复数是个复杂的问题，不同语言对于复数有不同的规则。使用管道符 <code v-pre>|</code> ，可以区分字符串的单复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'There is one apple|There are many apples'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你甚至可以创建更复杂的复数规则，为多个数字范围指定翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|[1,19] There are some|[20,*] There are many'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在定义具有复数选项的翻译字符串之后，你可以使用 <code v-pre>trans_choice</code> 方法来检索给定「数量」的内容。在这个例子中，设置「总数」为 10 ，符合数量范围 1 至 19，所以会得到 <code v-pre>There are some</code> 这条复数语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="重写扩展包的语言文件" tabindex="-1"><a class="header-anchor" href="#重写扩展包的语言文件"><span>重写扩展包的语言文件</span></a></h2>
<p>部分扩展包可能会附带自己的语言文件。你可以通过在 <code v-pre>resources/lang/vendor/{package}/{locale}</code> 放置文件来重写它们，而不是直接修改扩展包的核心文件。</p>
<p>例如，你需要重写 <code v-pre>skyrim/hearthfire</code> 扩展包的英文语言文件 <code v-pre>messages.php</code> ，则需要把文件放置在 <code v-pre>resources/lang/vendor/hearthfire/en/messages.php</code> 。在这个文件中只应定义要覆盖的翻译字符串。任何没有被覆盖的翻译字符串仍将从扩展包的原始语言文件中加载。</p>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


